<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>快递单号查询</title>
    <style>
      .search-box {
        position: relative;
        width: 178px;
        margin: 100px;
      }

      .search-box > .prompt-box {
        display: none;
        position: absolute;
        top: -40px;
        width: 171px;
        line-height: 20px;
        padding: 5px 0;
        color: #333333;
        font-size: 18px;
        border: 1px solid rgba(0, 0, 0, 0.2);
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
      }
      /* 三角形 */
      .search-box > .prompt-box::before {
        content: '';
        position: absolute;
        top: 28px;
        left: 18px;
        width: 0;
        height: 0;
        border: 8px solid #000000;
        border-style: solid dashed dashed;
        border-color: #ffffff transparent transparent;
      }

      input {
        outline: none;
      }
    </style>
  </head>
  <body>
    <div class="search-box">
      <div class="prompt-box"></div>
      <input type="text" placeholder="请输入快递单号" class="order" />
    </div>

    <script>
      var prompt = document.querySelector('.prompt-box');
      var order = document.querySelector('.order');

      // 按s键文本框获得焦点
      document.addEventListener('keyup', function (e) {
        // s键对应的数字是83
        if (e.keyCode === 83) {
          order.focus();
        }
      });

      order.addEventListener('keyup', function () {
        if (this.value === '') {
          prompt.style.display = 'none';
        } else {
          prompt.style.display = 'block';
          prompt.innerHTML = this.value;
        }
      });

      // 文本框失去焦点，prompt盒子隐藏
      order.addEventListener('blur', function () {
        prompt.style.display = 'none';
      });
      // 文本框获得焦点，prompt盒子显示
      order.addEventListener('focus', function () {
        if (this.value !== '') {
          prompt.style.display = 'block';
        }
      });
    </script>
  </body>
</html>
